package indi.matcha.csmallproducttest.service.impl;

import indi.matcha.csmallproducttest.ex.ServiceException;
import indi.matcha.csmallproducttest.mapper.PmsAttributeTemplateMapper;
import indi.matcha.csmallproducttest.pojo.dto.PmsAttributeTemplateDTO;
import indi.matcha.csmallproducttest.pojo.entity.PmsAttributeTemplate;
import indi.matcha.csmallproducttest.service.PmsAttributeTemplateService;
import indi.matcha.csmallproducttest.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: csmall-product
 * @description:
 * @author: Mr.Tang
 * @create: 2023-03-23 19:09
 **/

@Service
public class PmsAttributeTemplateServiceImpl implements PmsAttributeTemplateService {
    @Autowired
    PmsAttributeTemplateMapper pmsAttributeTemplateMapper;
    @Override
    public void addNew(PmsAttributeTemplateDTO pmsAttributeTemplateDTO) {
        String name = pmsAttributeTemplateDTO.getName();
        int countByName = pmsAttributeTemplateMapper.countByName(name);
        if (countByName > 0){
            String message = "添加属性模板失败,属性模板名称已经存在！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        System.out.println("添加属性模板！");
        PmsAttributeTemplate pmsAttributeTemplate = new PmsAttributeTemplate();
        BeanUtils.copyProperties(pmsAttributeTemplateDTO,pmsAttributeTemplate);
        pmsAttributeTemplateMapper.insert(pmsAttributeTemplate);
    }
}
